package object

import (
	"io"
	"fmt"
	"ones-objects/config"
	"net/http"
)

type GetStream struct {
	reader io.Reader
}

func NewGetStream(server, name string) (*GetStream, error) {
	if len(server) <= 0 || len(name) <= 0 {
		return nil,fmt.Errorf("invalid param server %s name %s", server, name)
	}
	return getStream(config.HTTP_PROTOCAL + server + config.OBJECTS_PATH + name)
}
func getStream(url string) (*GetStream, error) {
	res,err := http.Get(url)
	if err != nil {
		return nil,err
	}
	if res.StatusCode != http.StatusOK {
		return nil,fmt.Errorf("dataserver return status is %s", res.StatusCode)
	}
	return &GetStream{res.Body},nil
}

func (rd *GetStream) Read(p []byte) (n int, err error) {
	return rd.Read(p)
}
